#To load dataset in data object from local directory
data <- read.csv("D:/TATVIC-ML/R-code/ds/product_revenue.csv")

#To check summary of dataset
summary(data)

#To check total numbers of rows
nrow(data)

#To get data variables in separate R variable
yitemrevenue <- data$yitemrevenue
xcartadd <- data$xcartadd
xcartuniqadd <- data$xcartuniqadd
xcartaddtotalrs <- data$xcartaddtotalrs
xcartremove <- data$xcartremove
xcardtremovetotal <- data$xcardtremovetotal
xcardtremovetotalrs <- data$xcardtremovetotalrs
xproductviews <- data$xproductviews
xuniqprodview <- data$xuniqprodview
xprodviewinrs <- data$xprodviewinrs

#To check distribution with histogram function
hist(xcartadd) 
hist(yitemrevenue)
hist(xcartuniqadd) 
hist(xcartaddtotalrs) 
hist(xcartremove) 
hist(xcardtremovetotal) 
hist(xcardtremovetotalrs)
hist(xproductviews) 
hist(xuniqprodview) 
hist(xprodviewinrs) 

#To develop Regression model for with ourliers dataset
model <- lm(yitemrevenue ~ xcartadd + xcartuniqadd + xcartaddtotalrs + xcartremove + xcardtremovetotal + xcardtremovetotalrs + xproductviews + xuniqprodview + xprodviewinrs )


#To make new dataset object with data filteration with certain condition
newdata <- subset(data,xcartadd<200 & xcartuniqadd<100 & xcartaddtotalrs<2e+05 & xcartremove<5 & xcardtremovetotal<5 & xcardtremovetotalrs<5000 & xproductviews <5000 & xuniqprodview<2500 )

#To check summary of newdata object
summary(newdata)

#To store data in new data variable with _out postfix
yitemrevenue_out <- newdata$yitemrevenue
xcartadd_out <- newdata$xcartadd
xcartuniqadd_out <- newdata$xcartuniqadd
xcartaddtotalrs_out <- newdata$xcartaddtotalrs
xcartremove_out <- newdata$xcartremove
xcardtremovetotal_out <- newdata$xcardtremovetotal
xcardtremovetotalrs_out <- newdata$xcardtremovetotalrs
xproductviews_out <- newdata$xproductviews
xuniqprodview_out <- newdata$xuniqprodview
xprodviewinrs_out <- newdata$xprodviewinrs



#new dataframe without outliers
out_df <- data.frame(yitemrevenue_out,xcartaddtotalrs_out,xcartremove_out,xproductviews_out,xuniqprodview_out,xprodviewinrs_out)

#model for without outliers dataset
model_out <- lm(formula=yitemrevenue_out ~ xcartaddtotalrs_out + xcartremove_out + xproductviews_out + xuniqprodview_out + xprodviewinrs_out,out_df )

#To variable selection on model_out by MASS package and stepAIC method
library(MASS)
stepAIC(model_out,direction='backward')

#To variable selection on model_out by leaps package and regsubsets method
library(leaps)
leaps <- regsubsets(yitemrevenue_out ~ xcartadd_out + xcartuniqadd_out + xcartaddtotalrs_out + xcartremove_out + xcardtremovetotal_out + xcardtremovetotalrs_out + xproductviews_out + xuniqprodview_out + xprodviewinrs_out,data= newdata)
plot(leaps,scale="adjr2")

#To check AIC of both model for model comparision
AIC(model,model_out)

#shrinkage method for cross validation
shrinkage <- function(fit, k=10){
require(bootstrap)
theta.fit <- function(x,y){lsfit(x,y)}
theta.predict <- function(fit,x){cbind(1,x)%*%fit$coef}
x <- fit$model[,2:ncol(fit$model)]
y <- fit$model[,1]
results <- crossval(x, y, theta.fit, theta.predict, ngroup=k)
r2 <- cor(y, fit$fitted.values)^2
r2cv <- cor(y, results$cv.fit)^2
cat("Original R-square =", r2, "\n")
cat(k, "Fold Cross-Validated R-square =", r2cv, "\n")
cat("Change =", r2-r2cv, "\n")
}

# Cross checking with shrinkage method on model and model_out
shrinkage(model)
shrinkage(model_out)

# you can get more information at http://www.tatvic.com/blog/product-revenue-prediction-with-r-part-3 for getting prediction from this model.